package com.wcity.manage.web.block;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.BlockMapper;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.BlockService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by jn on 2016/12/13.
 * 小区信息后台管理ctrl
 */
@Controller
public class BlockAct {

    /**
     * 批量导入跳转
     * @return
     */
    @RequestMapping("to_block_import")
    public ModelAndView toBlockImport(){
        return new ModelAndView("block/import");
    }

    /**
     * 批量导入
     * @param excel
     * @param request
     * @param response
     */
    @RequestMapping("block_import")
    public void blockImport(@RequestParam CommonsMultipartFile excel,
                            HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        String errorMsg = blockService.blockImportTest( excel, user );
        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "导入成功", "block_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 批量导出
     * @param block
     * @param request
     * @param response
     */
    @RequestMapping("block_export")
    public void blockExport(Block block, HttpServletRequest request, HttpServletResponse response){
        response.reset();
        try {
            response.addHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode("小区信息.xlsx","utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        block.setUserId( user.getId() );

        try {
            blockService.blockExport( block, response.getOutputStream() );
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除小区
     * @param id
     * @param response
     */
    @RequestMapping("block_delete")
    public void blockDelete(String id,HttpServletResponse response){

        String errorMsg = blockService.blockDel( Integer.parseInt( id ) );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "删除成功", "block_list", "", "", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 修改小区
     * @param block
     * @param response
     */
    @RequestMapping("block_edit")
    public void blockEdit(Block block,HttpServletResponse response){

        String errorMsg = blockService.blockEdit( block );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "block_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 修改小区信息跳转
     * @param id
     * @return
     */
    @RequestMapping("to_block_edit")
    public ModelAndView toBlockEdit(String id){
        Block block = blockMapper.find( Integer.parseInt( id ) );
        ModelAndView mav = new ModelAndView("block/blockedit");
        mav.addObject("block",block);
        return mav;
    }

    /**
     * 新增小区
     * @param block
     * @param request
     * @param response
     */
    @RequestMapping("block_add")
    public void blockAdd(Block block, HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );

        String errorMsg = blockService.blockAdd( block, user );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "block_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 新增小区跳转
     * @return
     */
    @RequestMapping("to_block_add")
    public ModelAndView toBlockAdd(){
        return new ModelAndView("block/blockadd");
    }

    /**
     * 小区信息列表
     * @param pageModel
     * @param block
     * @param request
     * @return
     */
    @RequestMapping("block_list")
    public ModelAndView blockList(PageModel<Block> pageModel, Block block, HttpServletRequest request){

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        block.setUserId( user.getId() );
        pageModel.setParams( block );

        List<Block> list = blockMapper.findListPage( pageModel );

        ModelAndView mav = new ModelAndView("block/blocklist");
        mav.addObject("pageModel",pageModel);
        mav.addObject("list",list);
        mav.addObject("block",block);
        return mav;
    }

    @Resource private BlockMapper blockMapper;
    @Resource private BlockService blockService;
}
